package cn.bgs.service.impl;

import cn.bgs.common.R;
import cn.bgs.pojo.VolunteerService;
import cn.bgs.mapper.VolunteerServiceMapper;
import cn.bgs.service.VolunteerServiceService;
import cn.bgs.vo.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 志愿服务表 服务实现类
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@Service
public class VolunteerServiceServiceImpl extends ServiceImpl<VolunteerServiceMapper, VolunteerService> implements VolunteerServiceService {

    @Autowired
    private VolunteerServiceMapper volunteerServiceMapper;

    @Override
    public R findVolunteerService(String styShowTitle,String styShowState) {

        List<VolunteerService> volunteerServiceList = volunteerServiceMapper.findVolunteerService(styShowTitle, styShowState);

        return R.ok().data("volunteerServiceList",volunteerServiceList);
    }

    @Override
    public R findVolunteerServiceId(Integer id) {

        VolunteerService volunteerService = volunteerServiceMapper.findVolunteerServiceId(id);

        return R.ok().data("volunteerService",volunteerService);
    }

    @Override
    public R addVolunteerService(VolunteerService volunteerService) {

        volunteerService.setStyShowNumber(RandomUtil.getSixBitRandom());

        Integer i = volunteerServiceMapper.addVolunteerService(volunteerService);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","新增失败");
        }
    }

    @Override
    public R updateVolunteerService(VolunteerService volunteerService) {

        Integer i = volunteerServiceMapper.updateVolunteerService(volunteerService);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","编辑失败");
        }
    }

    @Override
    public R delVolunteerService(Integer id) {

        VolunteerService volunteerService = volunteerServiceMapper.findVolunteerServiceId(id);

        volunteerService.setDeleteFlag(1);

        Integer i = volunteerServiceMapper.updateVolunteerService(volunteerService);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","删除失败");
        }
    }
}
